#include "read_text_file.h"

GLchar* readTextFile(const char* name)
{
	FILE* fp;
	GLchar* content = NULL;
	int count = 0;

	/* verify that we were actuall given a name */
	if (!name) return NULL;

	/* attemp to open the file */
	fp = fopen(name, "rt");
	if (!fp) return NULL;

	/* determine the length of the file */
	fseek(fp, 0, SEEK_END);
	count = ftell(fp);
	rewind(fp);

	/* allocate a buffer and read the file into it */
	if (count > 0) {
		content = (GLchar*)malloc(sizeof(GLchar) * (count + 1));
		if (content) {
			fread(content, sizeof(GLchar), count, fp);
			content[count] = '\0';
		}
	}

	fclose(fp);
	return content;
}
